/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 02.01.2006
 *
 *************************************************/
package fightplan.persistence;

import static fightplan.persistence.ImportHelper.getString;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import fightplan.business.Category;
import fightplan.business.CategoryType;
import fightplan.business.Gender;
import fightplan.business.Tournament;

public class ExcelCategoryImport {

	public static void imprtCategories(Tournament tournament, File file, boolean importNames) {
		try {
			imprtCategories(tournament, new FileInputStream(file), importNames);
		} catch (Exception x) {
			x.printStackTrace();
			JOptionPane.showMessageDialog(null, "Kategorie Import fehlgeschlagen: " + x.getMessage(), "Fehler",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public static void imprtCategories(Tournament tournament, InputStream inputStream, boolean importNames) {
		try {
			POIFSFileSystem fs = new POIFSFileSystem(inputStream);
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			HSSFSheet sheet = wb.getSheetAt(0);
			int pos = 1;
			List<Category> categories = new ArrayList<Category>();
			while (pos <= sheet.getLastRowNum()) {
				HSSFRow row = sheet.getRow(pos++);
				if (row == null)
					continue;

				String name = getString(row, 0);
				if (name == null || name.length() == 0)
					continue;

				Category category = new Category();
				if (importNames)
					category.setName(name);
				category.setCategoryType(CategoryType.parseType(getString(row, 1)));
				category.setGender(Gender.parse(getString(row, 2)));
				category.setAgeRangeFormatted(getString(row, 3));
				category.setWeightRangeFormatted(getString(row, 4));
				category.setBeltRangeFormatted(getString(row, 5));
				categories.add(category);
			}
			/*
			 * String doubleNames = null; for (int i = 0; i<categories.size();
			 * i++) { String name1 = categories.get(i).getName().trim(); for
			 * (int j = i + 1; j<categories.size(); j++) { String name2 =
			 * categories.get(j).getName().trim(); if
			 * (name1.equalsIgnoreCase(name2)) { if (doubleNames == null) {
			 * doubleNames = name1; } else if (!doubleNames.contains(name1)) {
			 * doubleNames += "\n" + name1; } } } } if (doubleNames != null) {
			 * FPExceptionHandler.instance().showWarning(
			 * "Die Excel-Liste enthült min 2 Kategorien mit Namen\n" +
			 * doubleNames + "\nDaher wurde die Import nicht ausgeführt.",
			 * "Import fehlgeschlagen"); return; } String existingNames = null;
			 * for (int i = 0; i<categories.size(); i++) { String name1 =
			 * categories.get(i).getName().trim(); for (int j = 0;
			 * j<tournament.getCategories().size(); j++) { String name2 =
			 * tournament.getCategories().get(j).getName().trim(); if
			 * (name1.equalsIgnoreCase(name2)) { if (existingNames == null) {
			 * existingNames = name1; } else if
			 * (!existingNames.contains(name1)){ existingNames += "\n" + name1;
			 * } } } } if (existingNames != null) {
			 * FPExceptionHandler.instance().showWarning(
			 * "In der Excel-Liste sind folgende\nKategorien enthalten, die schon existieren:\n"
			 * + existingNames + "\nDaher wurde die Import nicht ausgeführt.",
			 * "Import fehlgeschlagen"); return; }
			 */
			tournament.getCategories().addAll(categories);
		} catch (Exception x) {
			x.printStackTrace();
			JOptionPane.showMessageDialog(null, "Kategorie Import fehlgeschlagen: " + x.getMessage(), "Fehler",
					JOptionPane.ERROR_MESSAGE);
		}
	}

}
